package com.example.forum.interceptor;

import com.example.forum.config.AppConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session
        HttpSession session = request.getSession(false);
        //判断session值
        if (session != null || session.getAttribute(AppConfig.USER_SESSION) != null){
            return true;
        }
        //强制用户登录
        response.sendRedirect("/sign-in.html");
        return false;
    }
}
